package com.mall.web.service;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.mall.pay.config.PayConfig;
import com.mall.untils.MapUtil;

/**
 * @author ycs
 *	2016年7月20日10:09:39
 *
 */
@Service("aLiPayService")
public class ALiPayService {
	
	private Logger logger = LoggerFactory.getLogger(ALiPayService.class);
	@Resource JdbcTemplate jdbcTemplate;
	
		
	/**
	 * 拼接页面
	 * @param payUrl				具体地址
	 * @param platformType			平台类型
	 * @param WIDout_trade_no		订单号
	 * @param WIDsubject			订单名称
	 * @param WIDtotal_fee			订单金额
	 * @param WIDbody				订单描述
	 * @param WIDshow_url			商品地址
	 * @return
	 */
	public StringBuffer getAliPayHtml(String payUrl,String platformType, String orderType,String WIDout_trade_no,String WIDsubject,String WIDtotal_fee,String WIDbody,String WIDshow_url){
		StringBuffer sbHtml = new StringBuffer();
        sbHtml.append("<form id=\"alipaysubmit\" name=\"alipaysubmit\" action=\"" + payUrl
                + "\" method=\"" + "post"
                + "\">");    
        sbHtml.append("<input type=\"hidden\" name=\"platformType\" value=\"" + platformType + "\"/>");
        sbHtml.append("<input type=\"hidden\" name=\"orderType\" value=\"" + orderType + "\"/>");
        sbHtml.append("<input type=\"hidden\" name=\"WIDout_trade_no\" value=\"" + WIDout_trade_no + "\"/>");
        sbHtml.append("<input type=\"hidden\" name=\"WIDsubject\" value=\"" + WIDsubject + "\"/>");
        sbHtml.append("<input type=\"hidden\" name=\"WIDtotal_fee\" value=\"" + WIDtotal_fee + "\"/>");
        sbHtml.append("<input type=\"hidden\" name=\"WIDbody\" value=\"" + WIDbody + "\"/>");
        sbHtml.append("<input type=\"hidden\" name=\"WIDshow_url\" value=\"" + WIDshow_url + "\"/>");
        
        
       
        //submit按钮控件请不要含有name属性
        sbHtml.append("<input type=\"submit\" value=\"确认\" style=\"display:none;\"></form>");
        sbHtml.append("<script>document.forms['alipaysubmit'].submit();</script>");
        return sbHtml;
	}
	
	/**
	 * 返回页面
	 * @param request
	 * @param response
	 * @param sHtmlText			页面具体信息
	 */
	public void outputStream(HttpServletRequest request, HttpServletResponse response,String sHtmlText){
        response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
        
		Writer write;
		try {
			write = response.getWriter();
			write.write(sHtmlText);
			write.flush();
			write.close();		
		} catch (IOException e) {
			logger.info("数据流异常"+e.getMessage());
		}
	}
			
	//添加新的支付宝记录	
	public void addRecord(Map<String, String> params,int pay_type ){
		String sql="INSERT INTO `tb_pay_record` (`platform_type`, `pay_type`, `orderId`, `total_fee`, `order_name`, `buyer_id`, `trade_no`,`create_date`, `update_date`) VALUES (?, ?, ?, ?, ?, ?, ?, now(), now())";
		String platform_type="";
		if(pay_type==11){
			platform_type=MapUtil.getStringValue(params, "body").split("-")[0];
			
		}else if(pay_type==12){
			platform_type=MapUtil.getStringValue(params, "extra_common_param").split("-")[0];
		}
		
		String orderId=MapUtil.getStringValue(params, "out_trade_no");
		Double total_fee=MapUtil.getDoubleValue(params, "total_fee");
		String order_name=MapUtil.getStringValue(params, "subject");
		String buyer_id=MapUtil.getStringValue(params, "buyer_email");
		String trade_no=MapUtil.getStringValue(params, "trade_no");
		jdbcTemplate.update(sql, platform_type,pay_type,orderId,total_fee,order_name,buyer_id,trade_no);		
	}
	
	
	//获取回调地址
	public Map<String, Object> getCodeurl(String platform_type, String pay_type){
		Map<String, Object> configMap = PayConfig.getWxpayConfig(platform_type, pay_type);
		return configMap;
	}
	
	
}
